<?php
require("bases.php");

class schoolweek extends bases {

	public function __construct(){
		parent::__construct();
		try{
			$this->fmenu = $this->controller = "schoolweek";
			$this->position_one = array("text"=>"教学周管理");

			$obj_school_year = spClass('school_year');
			$condition = "1 and isdel = 0";
			$this->school_year = $obj_school_year->findAll($condition,null,'id,name');
		}catch(Exception $e){

		}
	}

	public function datalist(){
		$obj_school_week = spClass('school_week');
		$obj_school_year = spClass('school_year');
		$postdata = $this->spArgs();
		$page = intval(max($postdata['page'], 1));
		$condition = "1 and sys_school_weeks.isdel = 0";
		if($searchkeys = $postdata['searchkeys']){
			$condition .= " and (sys_school_weeks.name like '%{$searchkeys}%' )";
			$this->searchkeys = $searchkeys;
		}
		
		$data_rs = $obj_school_week ->join("sys_school_year","sys_school_weeks.school_year_id = sys_school_year.id")
			->spPager($page, 10)->findAll($condition,  'id desc', "sys_school_weeks.* , sys_school_year.name as school_year_name");
		
		$this->data_rs = $data_rs;
		$this->pager = $obj_school_week->spPager()->getPager();
		$this->url = spUrl($this->controller, 'datalist', array("searchkeys"=>$this->searchkeys));
		$this->position_two = array("text"=>"教学周管理", "url"=>spUrl($this->controller, "datalist"));
		$this->formtitle = array("text"=>"教学周");
		$this->searchform = 1;
	}

	public function create(){
		$obj_school_week = spClass('school_week');
		$obj_school_year = spClass('school_year');
		if($_SERVER['REQUEST_METHOD'] == 'POST'){
			try{
				$postdate = $this->spArgs();
				$data = array();
				//客户端提交方法
				$data['name'] = $postdate['name'];
				$data['starttime'] = date("Y-m-d", strtotime($postdate['starttime']));
				$data['endtime'] = date("Y-m-d", strtotime($postdate['endtime']));
				$data['school_year_id'] = intval($postdate['school_year_id']);
				$data['status'] = intval($postdate['status']);
				if($result = $obj_school_week->spValidator($data)) {
					foreach($result as $item) {
						throw new Exception($item[0]);
					}
				}
				$unix_starttime = strtotime($postdate['starttime']);
				$unix_endtime = strtotime($postdate['endtime']);
				if(!$obj_school_week->checkEndtime($unix_starttime, $unix_endtime)){
					throw new Exception("教学周结束时间必须有大于开始时间！");
				}
				if(!$obj_school_week ->checkNextStarttime($unix_endtime)){
					throw new Exception("新增教学周开始时间必须大于上一教学周结束时间！");
				}
				if(!$obj_school_week->create($data))
					throw new Exception("未知错误，教学周添加失败");
				$url = spUrl($this->controller, "datalist");
				$message = array('msg'=>'教学周添加成功！','result'=>1, 'url'=>$url);
				echo json_encode($message);
				exit();
			}catch(Exception $e){
				$message = array('msg'=>$e->getMessage() ,'result'=>-1);
				echo json_encode($message);
				exit();
			}
		}
	
		$this->saveurl = spUrl($this->controller, "create");
		$this->position_two = array("text"=>"教学周添加", "url"=>spUrl($this->controller, "datalist"));
		$this->formtitle = array("text"=>"教学周添加");
	}

	function modify(){
		try {
			$postdata = $this->spArgs();
			$obj_school_week = spClass("school_week");
			$obj_school_year = spClass("school_year");
			if(!$id = intval($postdata['id']))
				throw new Exception("非法操作");
			if(!$info_rs = $obj_school_week->find(array("id"=>$id)))
				throw new Exception("找不到该教学周信息，可能已被删除");
			
			if($_SERVER['REQUEST_METHOD'] == 'POST'){
				try{
					$data = array();
					//客户端提交方法
					$data['name'] = $postdata['name'];
					$data['starttime'] = date("Y-m-d", strtotime($postdata['starttime']));
					$data['endtime'] = date("Y-m-d", strtotime($postdata['endtime']));
					$data['school_year_id'] = intval($postdata['school_year_id']);
					$data['status'] = intval($postdata['status']);
					if($result = $obj_school_week->spValidator($data)) {
						foreach($result as $item) {
							throw new Exception($item[0]);
						}
					}
					$unix_starttime = strtotime($postdata['starttime']);
					$unix_endtime = strtotime($postdata['endtime']);
					if(!$obj_school_week->checkEndtime($unix_starttime, $unix_endtime)){
						throw new Exception("教学周结束时间必须有大于开始时间！");
					}
					if(!$obj_school_week ->checkNextStarttime($unix_endtime,$id)){
						throw new Exception("教学周开始时间必须大于上一教学周结束时间！");
					}
					if(!$obj_school_week->update(array("id"=>$id), $data))
						throw new Exception("未知错误，学年信息更新失败");
					$url = spUrl($this->controller, "datalist");
					$message = array('msg'=>'修改成功！','result'=>0, 'url'=>$url);
					echo json_encode($message);
					exit();
				}catch(Exception $e){
					$message = array('msg'=>$e->getMessage() ,'result'=>-1);
					echo json_encode($message);
					exit();
				}
			}
		}catch(Exception $e){
			$this->redirect(spUrl($this->controller, "datalist"), "非法操作！");
		}
		$this->id = $id;
		$this->info_rs = $info_rs;
		$this->saveurl = spUrl($this->controller, "modify");
		$this->position_two = array("text"=>"教学周修改", "url"=>spUrl($this->controller, "datalist"));
		$this->formtitle = array("text"=>"教学周修改");
		$this->display($this->controller."/create.html");
	}

	public function delete(){
		$id = $this->spArgs('id');
		if($id){
			$obj_school_week = spClass('school_week');
			$url = spUrl($this->controller, "datalist");
			if(is_array($id)){
				foreach ($id as $v){
					$conditions = array('id' => $v);
					$obj_school_week->update($conditions, array('isdel' => 1));
				}
				echo json_encode(array('msg'=>'删除成功！' , 'result'=>1, 'url'=>$url));
				exit();
			}
			else {
				$conditions = array('id' => $id);
				if($obj_school_week->update($conditions,array('isdel' =>1))){
					echo json_encode(array('msg'=>'删除成功！' , 'result'=>1, 'url'=>$url));
					exit();
				}
				echo json_encode(array('msg'=>'删除失败！' , 'result'=>0, 'url'=>$url));
				exit();
			}
		}else{
			echo json_encode(array('msg'=>'请先选择删除项，再进行操作！' , 'result'=>0));
			exit();
		}
	}
	
	

}
?>